import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import { Router} from '@angular/router';
import { LoginService } from '../service/login.service';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;
  constructor(private fb: FormBuilder , private loginService: LoginService, private router: Router) {
    this.loginForm = this.fb.group({
      userName: ['', [Validators.required]],
      passWord: ['', [Validators.required]],

    });
  }
  ngOnInit() {
  }
  login({ value, valid}) {
     if (!valid) {
       return;
     }
     this.loginService.login(value).subscribe(
       () => {
         if (this.loginService.isLoginIn()) {
          const redirectUrl = this.loginService.redirectUrl ? this.loginService.redirectUrl : '/';
          this.router.navigate([redirectUrl]);
         }
       }
     )
  }
}
